package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.repository.MovieRepository;
import com.example.moviecms2.utils.ResultVoUtils;
import com.example.moviecms2.vo.MovieVo;
import com.example.moviecms2.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/movie")
public class MovieRestfulController {

    @Autowired
    private MovieRepository movieRepository;

    //查询列表
    @RequestMapping(value = "/lists")
    public ResultVo<List<Movie>> lists() {
        List<Movie> list = movieRepository.findAll();

        List<MovieVo> list1 = new ArrayList<>();
        for (Movie movie : list) {
            MovieVo vo = new MovieVo();
            vo.setId(movie.getId());
            vo.setName(movie.getName());
            vo.setPhoto(movie.getPhoto());
            list1.add(vo);
        }

        ResultVo resultVo = new ResultVo();
        resultVo.setCode(200);
        resultVo.setMsg("success");
        resultVo.setData(list1);
        return resultVo;
    }

    //查询一个
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResultVo<MovieVo> detail(@PathVariable Long id) {
        Movie movie = movieRepository.findOne(id);
        MovieVo movieVo = new MovieVo(movie);
        return ResultVoUtils.success(movieVo);
    }

    //删除
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResultVo<String> delete(@PathVariable Long id) {
        movieRepository.delete(id);
        return ResultVoUtils.success(null);
    }

}
